// pages/hot/hot.js
const app = getApp()
var url = require('../../utils/url.js');
var page = 1;
var last_page = 0;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    console.log(page)
    wx.request({
      url: url.URl + 'cate/treasure',
      data: { page: page, goods_salenum: 1 },
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var mgs=res.data.msg;
        if (res.data.code == 200) {
          if (res.data.data != "") {
            last_page = res.data.data.last_page
            var shops = res.data.data.data;
            var bill = "bill"
            for (var i = 0; i < shops.length; i++) {
              console.log(i)
              var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
              shops[i].bill = billF.toFixed(2) + "%"
            }
            console.log(shops)
            that.setData({
              shops: that.data.shops.concat(shops)
            })
            page++;
          }
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    });
  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
     shops:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var that=this;
     
  },
  getData(){
    var that=this;
    if (page > 1) {
      page--;
    }
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'cate/treasure',
      data: { page: page, goods_salenum: 1 },
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg;
        if (res.data.code == 200) {
          if(res.data.data!=""){
            last_page = res.data.data.last_page
            var shops = res.data.data.data;
            var bill = "bill"
            for (var i = 0; i < shops.length; i++) {
              console.log(i)
              var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
              shops[i].bill = billF.toFixed(2) + "%"
            }
            console.log(shops)
            that.setData({
              shops: shops
            })
          }
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    that.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var self = this;
    setTimeout(() => {
      // 模拟请求数据，并渲染
      page = 1
      self.getData()
      // 数据成功后，停止下拉刷新
      wx.stopPullDownRefresh();
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})